package com.tanhua.sso.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tanhua.sso.service.SendSms;
import org.springframework.stereotype.Component;

/**
 * @Author: Sabrina
 * @Date:2025/06/11/14:16
 * @Description:
 */
@Component
public class AliyunSendSms implements SendSms {
    @Override
    public String sendSms(String phoneNumber) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tR4Yq18QkFdgMgRmNGb", "Qo6Uxa7zHPBXd78uxCYx2iQaID2mMl");
        IAcsClient client = new DefaultAcsClient(profile);//客户端

        CommonRequest request = new CommonRequest();//盒子
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber); //目标手机号
        request.putQueryParameter("SignName", "阿里云短信测试"); //签名名称
        request.putQueryParameter("TemplateCode", "SMS_154950909"); //短信模板code
        request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");//模板中变量替换
        try {
            CommonResponse response = client.getCommonResponse(request);//客户端带着盒子发请求
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }
}
